﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

namespace CTEA_INDIA
{
    public partial class Candidate_Profile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["CTEA"].ConnectionString;
            int userid = Convert.ToInt32(Session["UserID"]);
            int login = Convert.ToInt32(Session["login"]);
            if (Session["UserId"] == null || Session["UserId"].ToString() == "")
            {
                Session.Abandon();
                Session.RemoveAll();
                Response.Redirect("Default.aspx");
            }
            if (!IsPostBack)
            {
                GetDeta();
                string[] Details = new string[2];

                Details = BAL.Common.GetLoginDetails(userid, login);
                spnUsername.Visible = true;
                spnLastUsedDate.Visible = true;
                spnUsername.InnerHtml = "Welcome  " + "<span class=\"spnWelcomeName\">" + Details[0] + "</span> ";
                spnLastUsedDate.InnerHtml = " Last used: " + Details[1];
               
            }
        }
        protected void IsNotFutureDate(object sender, ServerValidateEventArgs args)
        {
            DateTime dt;

            bool IsDate = DateTime.TryParse(args.Value.ToString(), out dt);

            if (IsDate)
            {
                if (dt.Date <= DateTime.Now.Date)
                    args.IsValid = true;
                else
                    args.IsValid = false;

            }

        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            Enabled();
            btnEdit.Visible = false;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Disable();
            btnEdit.Visible = true;
          //  GetDeta();
        }

        public void GetDeta()
        {

            int userid = Convert.ToInt32(Session["UserID"]);
            string[] details = BAL.Customers.GetCandidateProfile(userid);
            txtName.Text = details[1];
            ddlApplyFor.SelectedValue = details[2];
            txtPers_MobileNo.Text = details[3];
            txtAddress.Text = details[4];
            lblEmailID.Text = details[6];
            txtDOB.Text = details[7];
            ImgCandidate.ImageUrl = "ProfilePhoto/" + details[24];
            rbtGender.SelectedValue = details[8];
            txtLocation.Text = details[20];
            //lue = details[9];
            txtEducation.Text = details[9];
            txtQualification.Text = details[10];
            txtGuardian_Name.Text = details[11];
            txtGuardian_Locaton.Text = details[12];
            txtGuardian_Cno.Text = details[13];
            txtOccup.Text = details[15];
            txtRelationshp.Text = details[14];

            txthiddenphotoname.Text = details[24]; 

            Disable();



        }

        public void Enabled()
        {


            txtName.ReadOnly = false;
            ddlApplyFor.Enabled = true;
            txtPers_MobileNo.ReadOnly = false;
            txtAddress.ReadOnly = false;
            txtDOB.ReadOnly = false;
            ImgCandidate.Visible = true;
            rbtGender.Enabled = true;
            txtEducation.ReadOnly = false;
            txtQualification.ReadOnly = false;
            txtGuardian_Name.ReadOnly = false;
            txtGuardian_Locaton.ReadOnly = false;
            txtGuardian_Cno.ReadOnly = false;
            txtOccup.ReadOnly = false;
            txtRelationshp.ReadOnly = false;
            btnCancel.Visible = true;
            ftpProfpic.Enabled = true;
            txtLocation.ReadOnly = false;
            btnRegister.Visible = true;
        }


        public void Disable()
        {

            btnCancel.Visible = false;
            txtName.ReadOnly = true;
            ddlApplyFor.Enabled = false;
            txtPers_MobileNo.ReadOnly = true;
            txtAddress.ReadOnly = true;
            txtDOB.ReadOnly = true;
            ImgCandidate.Visible = true;
            rbtGender.Enabled = false;
            txtEducation.ReadOnly = true;
            txtQualification.ReadOnly = true;
            txtGuardian_Name.ReadOnly = true;
            txtGuardian_Locaton.ReadOnly = true;
            txtGuardian_Cno.ReadOnly = true;
            txtOccup.ReadOnly = true;
            txtRelationshp.ReadOnly = true;
            ftpProfpic.Enabled = false;
            txtLocation.ReadOnly = true;
            btnRegister.Visible = false;
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                string EmailID = lblEmailID.Text;
                //string Password = txtPass.Text;

                // string EncryptedPass = BAL.Encryption.Encript(Password, "&%#@?,:*");
                //string DecryptedPassword = BAL.Encryption.Decrypt(EncryptedPass, "&%#@?,:*");
                string Name = txtName.Text;
                string Gender = rbtGender.SelectedValue;
                DateTime DateOfBirth = Convert.ToDateTime(txtDOB.Text);
                string Address = txtAddress.Text;
                string Location = txtLocation.Text;
                string Mobileno = txtPers_MobileNo.Text;
                string Education = txtEducation.Text;
                string Qualification = txtQualification.Text;
                string ApplyFor = ddlApplyFor.SelectedValue;
                string Guardian_name = txtGuardian_Name.Text;
                string Guardian_Cno = txtGuardian_Cno.Text;
                string Occupation = txtOccup.Text;
                string Guardian_Location = txtGuardian_Locaton.Text;
                string Relationshp = txtRelationshp.Text;
                //  string filename = ftpProfpic.FileName;
                // string howKnow = txtHow_Approched.Text;

                string profphoto = ftpProfpic.FileName;
                if (profphoto == "")
                {


                    profphoto = txthiddenphotoname.Text;
                }
                else
                {
                    profphoto = ftpProfpic.FileName;
                    ftpProfpic.SaveAs(Server.MapPath("ProfilePhoto/" + profphoto));
                }
                BAL.Customers.UpdateCandidateProfile(Name, Gender, DateOfBirth, Address,
                                    Location, Mobileno, Education, Qualification, ApplyFor, Guardian_name, Guardian_Cno, Guardian_Location, Relationshp, profphoto, EmailID, Occupation);
                GetDeta();


                Disable();

                updatemessage.InnerHtml = ("Your Account Updated Successfully...!!");

                btnEdit.Visible = true;


            }
        }
    }
}
